package com.namecard.namecardapi.config;

import com.namecard.namecardapi.Interceptor.TokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author wzx
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/**").addResourceLocations("file:name-card-api/src/main/resources/static/img/");
    }

    @Override
    @Order(1)
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST")
                .allowedHeaders("*");
    }

    @Override
    @Order(2)
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器，并指定拦截的路径
        registry.addInterceptor(new TokenInterceptor())
                .excludePathPatterns("/cards/user/login")
                .excludePathPatterns("/cards/user/getRedisPushData")
                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**") // 放行Swagger
                .excludePathPatterns("/cards/user/test")
                .excludePathPatterns("/chat/msg/deleteChatRecord")
                .excludePathPatterns("/login/**")
                .excludePathPatterns("/**");
    }
}
